package pira.parser.http;

import java.util.ArrayList;

import pira.db.excute.DBScanner;

public class CiteURL extends PageParser {
	
	private DBScanner dbScanner;
	private ArrayList<Article> articles;
	
	public CiteURL(String paperURL) {
		super();
		this.url = paperURL;
		this.url = generateURL();
		try {
			this.readContent(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public CiteURL(String[] citeInfo, DBScanner dbScanner) {
		super();
		this.dbScanner = dbScanner;
		this.url = citeInfo[1];
		this.url = generateURL();
		this.articles= new ArrayList<Article>();
		try {
			this.readContent(url);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public ArrayList<Article> getArticles() {
		return articles;
	}
	
	@Override
	protected String generateURL() {
		url = CiteSeerX.PREFIX + url;
		return url;
	}

	@Override
	public void parse() {
		try {
			resultNum = getResultNumber();
//System.out.println("result Number is " + resultNum);

			if (resultNum == 0)
				System.out.println("There's no citation for the paper!");
			else if (resultNum <= 10) {
//				deleteFormerCitation();
				this.parseResult();
			} else {
//				deleteFormerCitation();
				int pageNum = (resultNum + 9) / 10;
				for (int i = 0; i < pageNum; i++) {
					this.parseResult();
					String newURL = getNextURL(i);

					readContent(newURL);
					long currentTime = System.currentTimeMillis();
					dbScanner.requestTime += (currentTime - dbScanner.sysTime);
					dbScanner.sysTime = currentTime;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String getNextURL(int i) {
		return url + SORTSUFFIX + (i + 1) * 10;
	}

	@Override
	protected boolean parseResult() {
		int index = -1;
		String title = null;
		String[] authors= null;
		Article article= null;
		
		if ((index = page.indexOf(RESULT)) != -1) {
			String mainContents = page.substring(index);
			String[] results = mainContents.split(BLOCKHIGHLIGHT);

			for (int i = 1; i < results.length; i++) {
				title = this.getTitle(results[i]);
				authors= this.getAuthors(results[i]);
				article= new Article(title, authors);
//System.out.println(i + " : " + title);

				long beforeDB = System.currentTimeMillis();
				dbScanner.requestTime += (beforeDB - dbScanner.sysTime);

//gdbui.addCitation(myNode, title);

				long afterDB = System.currentTimeMillis();
				dbScanner.dbTime += (afterDB - beforeDB);
				dbScanner.sysTime = afterDB;
				
				articles.add(article);
			}
		}
		return true;
	}

	public String[] getSelfAuthors() {
		String[] authors= null;
		int index = page.indexOf(AUTHORBAR2);
		if (index == -1) 
			return null;
		String s= page.substring(index);
		index = s.indexOf("</div>");
		s = s.substring(0, index);
		
		s = s.replaceAll("\\&[a-zA-Z]{1,10};", "").replaceAll(
				"<[^>]*>", "");
		s = s.replaceAll("[(/>)<]", "");
		//s = s.trim().replaceAll(",", "");
//System.out.println(s);
		authors = s.split(",");
		for (int i = 0; i < authors.length; i++) {
			authors[i] = authors[i].trim();
			if (i == 0) {
				authors[i] = authors[i].substring(3);
				authors[i] = authors[i].trim();
			}
//System.out.println(authors[i]);
		}
		return authors;
	}
}
